package com.drinks.yiniang.ui.fragment.message;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.message.GoodsMessageAdapter;
import com.drinks.yiniang.common.recycler.decoration.FirstItemDecoration;
import com.drinks.yiniang.entity.message.MessageJson;
import com.drinks.yiniang.io.api.MessageApiIO;
import com.drinks.yiniang.ui.activity.conversation.ServiceChatActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;

/**
 * 普通消息列表页面
 * @author han
 * @date 2019/09/10
 * A simple {@link Fragment} subclass.
 */
public class CommonMessageFragment extends BaseLazyFragment implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    //refresh
    private MSwipeRefreshLayout refresh_common_msg;
    //列表
    private BaseRecyclerView rcv_common_msg;

    private GoodsMessageAdapter mMessageAdapter;

    //页码
    private int mPages;

    public CommonMessageFragment() {
        // Required empty public constructor
    }

    public static CommonMessageFragment newInstance() {
        return new CommonMessageFragment();
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_common_message;
    }

    @Override
    protected void initView(View mView) {
        refresh_common_msg = mView.findViewById(R.id.refresh_common_msg);

        rcv_common_msg = mView.findViewById(R.id.rcv_common_msg);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        initAdapter();
    }

    private void initAdapter() {
        rcv_common_msg.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_common_msg.addItemDecoration(FirstItemDecoration.getItemDPDecoration(10));

        mMessageAdapter = new GoodsMessageAdapter(mContext, new ArrayList<MessageJson.MessageList>());
        mMessageAdapter.setMsgType(GoodsMessageAdapter.TYPE_MSG_COMMON_LIST);
        mMessageAdapter.setCenterEmpty(getLayoutInflater(), rcv_common_msg);
        mMessageAdapter.bindToRecyclerView(rcv_common_msg);

        ImageView iv_empty_view_icon = mMessageAdapter.getEmptyView().findViewById(R.id.iv_empty_view_icon);
        TextView tv_empty_view_name = mMessageAdapter.getEmptyView().findViewById(R.id.tv_empty_view_name);

        iv_empty_view_icon.setImageResource(R.mipmap.icon_msg_empty);
        tv_empty_view_name.setText(R.string.text_no_new_message);
    }

    @Override
    protected void listener() {
        refresh_common_msg.setOnRefreshListener(this);

        mMessageAdapter.setOnItemClickListener(this);
        mMessageAdapter.setOnLoadMoreListener(this, rcv_common_msg);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MessageJson.MessageList data = mMessageAdapter.getItem(position);

        if(data == null) {
            return;
        }

        Intent intent = new Intent(mContext, ServiceChatActivity.class);
        intent.putExtra(Constants.KeyUserId, data.getRelation_fans_id());
        intent.putExtra(Constants.KeyTitle, data.getUsername());
        startActivity(intent);
    }

    @Override
    protected void lazyLoadData() {
        getMessageList();
    }

    @Override
    public void onRefresh() {
        getMessageList();
    }

    @Override
    public void onLoadMoreRequested() {
        MessageApiIO.getInstance().getMessageList(Constants.TYPE_MSG_LIST_PRIVATE, mPages,
                new APIRequestCallback<MessageJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(MessageJson res) {
                        if (mMessageAdapter == null) {
                            return;
                        }

                        mPages++;

                        mMessageAdapter.addData(res.getData().getData());

                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mMessageAdapter.loadMoreComplete();
                        } else {
                            mMessageAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mMessageAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }
                });
    }

    //获取消息列表
    private void getMessageList() {
        if(!refresh_common_msg.isRefreshing()) {
            showLoadingDialog();
        }

        mPages = 1;

        MessageApiIO.getInstance().getMessageList(Constants.TYPE_MSG_LIST_PRIVATE, mPages,
                new APIRequestCallback<MessageJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(MessageJson res) {
                        if (mMessageAdapter == null) {
                            return;
                        }

                        mPages++;

                        mMessageAdapter.getData().clear();
                        mMessageAdapter.addData(res.getData().getData());

                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mMessageAdapter.loadMoreComplete();
                        } else {
                            mMessageAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mMessageAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        refresh_common_msg.setRefreshing(false);
                        dismissDialog();
                    }
                });
    }


}
